/**
 * *****************************************************************************
 * @file    adt3102_init.c
 * @author  Home(zhhuang@andartechs.com.cn)
 * @version 1.4.0
 * @date    2021-11-05
 * @brief
 *
 *                 Copyright (c) 2021, Andar Technologies Inc.
 *                           www.andartechs.com
 *
 * *****************************************************************************
 */
#include "adt3102_init.h"

#include "adt3102_system.h"
#include "adt3102_rx.h"
#include "adt3102.h"
#include "adt3102_uart.h"
#include "adt3102_timer.h"
#include "adt3102_pmu.h"
#include "adt3102_gpio.h"
#include "adt3102_tx.h"
#include "adt3102_type_define.h"
#include "gpio_ctype_map.h"
#include "rfc_ctype_map.h"
#include "scan.h"


/**
 * @brief   ADT3102 system initialization.
 * @param   None.
 * @return  None.
 * @note
 */
void adt3102SystemInit(void)
{
    //-----Initialize some basic functions -----
    setInterrupt(INT_UART1,     DISABLEINT);
    setInterrupt(INT_UART0,     ENABLEINT);
    setInterrupt(INT_TIMER0,    ENABLEINT);
    setInterrupt(INT_TIMER1,    ENABLEINT);
    setInterrupt(INT_DUALTIMER, ENABLEINT);
    setInterrupt(INT_FFT,       ENABLEINT);
    setInterrupt(INT_PMU,       ENABLEINT);
}


/**
 * @brief   ADT3102 value initialization.
 * @param   pDataStr: See the @AnalogDataStrTypedef member for details.
 * @return  None.
 * @note
 */
void adt3102ValInit(AnalogDataStrTypedef *pDataStr)
{
    /* rf */
    pDataStr->rx0En = ENABLE;
    pDataStr->rx1En = ENABLE;
    pDataStr->tx0En = DISABLE;
    pDataStr->tx1En = ENABLE;
    pDataStr->phaseShftLut0En = DISABLE;
    pDataStr->phaseShftLut1En = DISABLE;
    pDataStr->PaGain0   = 14;
    pDataStr->PaGain1   = 0;

    /* if */
    pDataStr->tiaHpfEn  = ENABLE;
    pDataStr->rcHpfEn   = ENABLE;
    pDataStr->pgaHpfEn  = ENABLE;
    pDataStr->tiaGain0  = TIA_GAIN_6DB; //0,6,12,18
    pDataStr->pgaGain0  = PGA_GAIN_44DB;//PGA_GAIN_30DB;
    pDataStr->tiaGain1  = TIA_GAIN_6DB;
    pDataStr->pgaGain1  = PGA_GAIN_44DB;//PGA_GAIN_30DB;
    pDataStr->tiaHpf    = TIA_HPF_300KHZ; //25,50,100,300
    pDataStr->rcHpf     = RC_HPF_400KHZ;   //150, 400
    pDataStr->pgaHpf    = PGA_HPF_500KHZ; //25,50,100,500
    pDataStr->pgaLpf    = PGA_LPF_4M;
}


/**
 * @brief   ADT3102 module initialization.
 * @param   pDataStr: See the @AnalogDataStrTypedef member for details.
 * @return  None.
 * @note
 */
void adt3102ModuleInit(void)
{
  
     AnalogDataStrTypedef *pDataStr = (AnalogDataStrTypedef *)(&(getScanStrPara()->chirpStr));
    //LED init
    //ledInit();

    //power on
    adt3102PowerSw(ON);

    //turn on RF and analog, config adc
    adt3102DigiInit();

    #if REMOVE_FIRST_CHIRP
        adt3102RfInit(pDataStr->rx0En, pDataStr->rx1En, pDataStr->tx0En, pDataStr->tx1En, pDataStr->phaseShftLut0En, pDataStr->phaseShftLut1En,CHIRP_NUM_FMCW_CONFIG + 1);
    #else
        adt3102RfInit(pDataStr->rx0En, pDataStr->rx1En, pDataStr->tx0En, pDataStr->tx1En, pDataStr->phaseShftLut0En, pDataStr->phaseShftLut1En, CHIRP_NUM_FMCW_CONFIG);
    #endif

    adt3102_tmp_Analog(pDataStr);

    #if REMOVE_FIRST_CHIRP
        adt3102AdcSetting(pDataStr->rx0En, pDataStr->rx1En, 1, ADC_SAMPLE_RATE, CHIRP_NUM_FMCW_CONFIG + 1);
    #else
        adt3102AdcSetting(pDataStr->rx0En, pDataStr->rx1En, 1, ADC_SAMPLE_RATE, CHIRP_NUM_FMCW_CONFIG);
    #endif

    if(HIGH_CLOCK == HIGH_CLOCK_125M)
    {
        switch125MHclk();
    }

    uartInit(UART0, 115200, HIGH_CLOCK);

    timerInit(Timer1, (float)FRAME_PERIOD, HIGH_CLOCK);

    //gpioDirection(9, 0);
    //gpioDirection(2, 0);
    // single tone
    // fmcwChirpSingleFrequency();
    // bistOn(BIST_AMP, BIST_DIV128_SEL);
    // siggen(0x1000, 0x0000, 0, 1, 0);
    // DSP->ds_adc_format_ctrl = ds_adc_format_ctrl_offset_auto_bit;
    // uint16 step, uint16 chgStep, uint16 comp, uint16 siggenEn, uint16 ang
}


/**
 * @brief   ADT3102 module initialization.
 * @param   pDataStr: See the @AnalogDataStrTypedef member for details.
 * @return  None.
 * @note
 */
void adt3102_tmp_Analog(AnalogDataStrTypedef *pDataStr)
{
    int32 biasRx0 = pDataStr->rx0En * 12;
    int32 biasRx1 = pDataStr->rx1En * 12;

    //enable pa1 and pa0
    txPaEnable(pDataStr->tx0En, pDataStr->tx1En);
    txPaBias(pDataStr->PaGain0, pDataStr->PaGain1);

    rxLnaEnable(pDataStr->rx0En, pDataStr->rx1En);
    mixerEnable(pDataStr->rx0En, pDataStr->rx1En);
    lnaRegularBias(pDataStr->rx0En, pDataStr->rx1En);
    lnaDacBias(biasRx0, biasRx0, biasRx0, biasRx1, biasRx1, biasRx1);

    //bias2_en: enable signal for ztc bias for BB
    baseBandBiasSetting(ENABLE);

    //turn on tia pga
    rfTiaEnable(pDataStr->rx0En, pDataStr->rx1En);
    rfTiaGainSetting(pDataStr->tiaGain0, pDataStr->tiaGain0, pDataStr->tiaGain1, pDataStr->tiaGain1);
    rfTiaDacBias(biasRx0, biasRx1);
    //pga config
    rfVgaEnable(pDataStr->rx0En, pDataStr->rx1En);
    rfVgaGainSetting(pDataStr->pgaGain0, pDataStr->pgaGain0, pDataStr->pgaGain1, pDataStr->pgaGain1);
    rfVgaDacBias(biasRx0, biasRx1);
    rfPgaGainCalEnable(0, 0, 0, 0);
    rfPgaGainCalSetting(PGA_CAL_P1, PGA_CAL_P1 ,PGA_CAL_P1 ,PGA_CAL_P1);
    //filter config
    tiaHpfConfig(pDataStr->tiaHpfEn, pDataStr->tiaHpfEn, pDataStr->tiaHpf, pDataStr->tiaHpf);
    rcHpfConfig(pDataStr->rcHpfEn, pDataStr->rcHpfEn, pDataStr->rcHpf, pDataStr->rcHpf);
    pgaLpfConfig(pDataStr->pgaLpf, pDataStr->pgaLpf);
    pgaHpfConfig(pDataStr->pgaHpfEn, pDataStr->pgaHpfEn, pDataStr->pgaHpf, pDataStr->pgaHpf);
    rfPhaseShiftLUT(pDataStr->phaseShftLut0En, pDataStr->phaseShftLut0En);

    #if DEBUGMODE
        // set P23 ~ P30 as GPIO's high-z. and then enable analog IF output.  
        gpioDirection(23,2);
        gpioDirection(24,2);
        gpioDirection(25,2);
        gpioDirection(26,2);
        gpioDirection(27,2);
        gpioDirection(28,2);
        gpioDirection(29,2);
        gpioDirection(30,2);
        
        //enable vga en_buffer
        RFC->rfc_vga0_reg |= 1 << 9;
        //en_buffer0 contorl if signal output gpio23, 24, 25, 26.
        //en_buffer1 contorl if signal output gpio27, 28, 29, 30.
        //when we test low power consumption, we need to connect p29 with vddrf enable.
        //Normally, p29 is high resistance, vga1 en_buffer will lower the p29 level.
        RFC->rfc_vga1_reg |= 1 << 9;
    #endif
}
